#include "vld.h"

#include "../sys/vid_impl.h"
#include "../common/com_console.h"

#include <windows.h>
#include "../sys/gl.h"

#include "../utils/FontPix.h"

bool appActive = false;
bool appExit = false;

CFontPix *font;

int DrawGLScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	font->startRender(640,480);
	font->moveTo(50,50);
	font->print("\\c0\\c1W\\c2W\\c3W\\c4W\\c5W\\c6W\\c7W");
	font->print("ayeeee");
	font->endRender();
	return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_ACTIVATE:
		if (!HIWORD(wParam))
			appActive=true;
		else
			appActive=false;
		return 0;

	case WM_SYSCOMMAND:
		switch (wParam)
		{
		case SC_SCREENSAVE:
		case SC_MONITORPOWER:
			return 0;
		}
		break;

	case WM_CLOSE:
		PostQuitMessage(0);
		return 0;

	case WM_SYSKEYDOWN:
	case WM_KEYDOWN:
		return 0;	

	case WM_SYSKEYUP:
	case WM_KEYUP:
		return 0;	

	case WM_MOVE:
		/*		int		xPos, yPos;
		RECT r;
		int		style;

		//			if (!wndCfg.fullscreen )
		{
		xPos = (short) LOWORD(lParam);    // horizontal position 
		yPos = (short) HIWORD(lParam);    // vertical position 

		r.left   = 0;
		r.top    = 0;
		r.right  = 1;
		r.bottom = 1;

		style = GetWindowLong( hWnd, GWL_STYLE );
		AdjustWindowRect( &r, style, FALSE );

		config->setValue_asNumber("vid_xpos",	xPos + r.left, CConfigFile::FLAG_NONE);
		config->setValue_asNumber("vid_ypos",	yPos + r.top, CConfigFile::FLAG_NONE);
		}

		*/
		return 0;
	}

	// Pass All Unhandled Messages To DefWindowProc
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	MSG msg;

	com_print("Hello world!");
	vid_init();

	font = new CFontPix();

	font->load("data/arial.ttf",12);

	while(!appExit)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if (msg.message==WM_QUIT)
			{
				appExit = true;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else
		{
			if (appActive)
			{
				DrawGLScene();
				vid_switchFrame();
			}
		}
	}

	delete font;

	vid_shutdown();
	return (int)(msg.wParam);
}